package com.lyz.user.controller;

import com.lyz.redis.util.CacheUtil;
import com.lyz.redis.util.RedisShareLockUtil;
import com.lyz.redis.util.RedisUtil;
import com.lyz.tool.ExportWordUtil;
import com.lyz.user.entity.po.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.PartialUpdate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CacheUtil cacheUtil;

    @Autowired
    private RedisShareLockUtil redisShareLockUtil;

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisUtil.set("name", "李诒泽");
    }

    @GetMapping("/testRedisLock")
    public String testRedisLock() {
        boolean result = redisShareLockUtil.lock("redisShareLock", "123456", 10000L);
        log.info("分布式锁获取：{}", result);
        return String.valueOf(result);
    }

    @GetMapping("/testLog")
    public void testLog() {
        long startTime = System.currentTimeMillis();
        for (int i = 1; i <= 10000; i++) {
            log.info("这是{}条日志", i);
        }
        long endTime = System.currentTimeMillis();
        log.info("当前打印日志耗时:{}ms", endTime - startTime);
    }

    @GetMapping("/testExport")
    public void testExport() throws Exception {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "经典鸡翅");
        dataMap.put("auditName", "可乐鸡翅");
        ExportWordUtil.exportWord(dataMap, "导出文件", "wordExport.ftl");
    }

    @GetMapping("/testLocalCache")
    public void testLocalCache(List<Long> skuIdList) {
        // list<Long> skuId
        // 价格，优惠券，限购 批量
        // Map<Long, SKUINFO>
        // 反射
//        List<Long> skuIdList = new ArrayList<>();
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
           Map<Long, SkuInfo> skuInfoMap = getSkuInfo(skuIdList);
           return skuInfoMap;
        });

        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuInfo.class, (list) -> {
            Map<Long, SkuPrice> skuInfoMap = getSkuPrice(skuIdList);
            return skuInfoMap;
        });
    }

    public Map<Long, SkuInfo> getSkuInfo(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    public Map<Long, SkuPrice> getSkuPrice(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    class SkuInfo {

        private Long id;

        private String name;

    }

    class SkuPrice {

        private Long id;

        private Long price;

    }

    @PostMapping("/testQuery")
    public void testQuery(@RequestBody SysUser sysUser) {
        // 2023-11-06 10:35:00
        System.out.println(sysUser);
    }

}
